package com.example.walkingtour.maps;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.example.walkingtour.PlayVideoActivity;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/*
 * Overlays are the coordinate pairs which are shown on the map.
 */
@SuppressWarnings("rawtypes")
public class TourMapItemizedOverlay extends ItemizedOverlay {

	private ArrayList<OverlayItem> mOverlays; // = new ArrayList<OverlayItem>();
	private Context mContext;

	public TourMapItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		this.mOverlays = new ArrayList<OverlayItem>();
	}

	public TourMapItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		this.mOverlays = new ArrayList<OverlayItem>();
	}

	public void Clear() {
		this.mOverlays.clear();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	/*
	 * Adds an overlay
	 */
	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	public void addCoordinates(String mFullFilePath, double mLatitude,
			double mLongitude) {
		GeoPoint mPoint = new LatLonPoint(mLatitude, mLongitude);
		OverlayItem overlayItem = new OverlayItem(mPoint, mFullFilePath,
				mLatitude + " " + mLongitude);
		addOverlay(overlayItem);
	}

	@Override
	public int size() {
		// populate();
		return mOverlays.size();
	}

	@Override
	protected boolean onTap(int index) {

		List<OverlayItem> samePairCoordinates = new ArrayList<OverlayItem>();
		OverlayItem item = mOverlays.get(index);

		samePairCoordinates = getSameCoordinatePair(item, samePairCoordinates);

		if (samePairCoordinates.size() > 1) {
			showDialog(samePairCoordinates);
		}

		else {
			String titleAndCoordinates = item.getTitle() + "\n"
					+ item.getSnippet(); // title and coordinates
											// concatenated into one string.
			Toast.makeText(mContext, titleAndCoordinates, Toast.LENGTH_LONG)
					.show();
			PlayVideoActivity.playVideo(mContext, item.getTitle()); // item.getTitle()is
																	// the full
																	// file path
																	// to the
																	// video
																	// file
																	// (usually
																	// on the SD
																	// card)
		}

		return super.onTap(index);
	}

	private void showCoordinateInfo(int index) {
		List<OverlayItem> samePairCoordinates = new ArrayList<OverlayItem>();
		OverlayItem item = mOverlays.get(index);

		samePairCoordinates = getSameCoordinatePair(item, samePairCoordinates);

		if (samePairCoordinates.size() > 1) {
			showDialog(samePairCoordinates);
		}

		else {
			String titleAndCoordinates = item.getTitle() + "\n"
					+ item.getSnippet(); // title and coordinates
											// concatenated into one string.
			Toast.makeText(mContext, titleAndCoordinates, Toast.LENGTH_LONG)
					.show();
			PlayVideoActivity.playVideo(mContext, item.getTitle()); // item.getTitle()is
																	// the full
																	// file path
																	// to the
																	// video
																	// file
																	// (usually
																	// on the SD
																	// card)
		}
	}

	private List<OverlayItem> getSameCoordinatePair(OverlayItem item,
			List<OverlayItem> samePairCoordinates) {
		for (OverlayItem searchItem : mOverlays) {
			if (isOverlayItemsSame(searchItem, item))
				samePairCoordinates.add(searchItem);
		}

		return samePairCoordinates;
	}

	private boolean isOverlayItemsSame(OverlayItem item1, OverlayItem item2) {
		boolean isSame = false;
		if (item1.getPoint().getLatitudeE6() == item2.getPoint()
				.getLatitudeE6())
			if (item1.getPoint().getLongitudeE6() == item2.getPoint()
					.getLongitudeE6())
				isSame = true;

		return isSame;
	}

	private void showDialog(List<OverlayItem> samePairCoordinates) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setCancelable(true);
		builder.setTitle("Choose coordinate");
		builder.setInverseBackgroundForced(true);

		final CharSequence[] chars = convertToChar(samePairCoordinates);
		builder.setItems(chars, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				PlayVideoActivity.playVideo(mContext, chars[which].toString());
				dialog.dismiss();
			}

		});

		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});

		AlertDialog alert = builder.create();
		alert.show();

	}

	private CharSequence[] convertToChar(List<OverlayItem> list) {
		CharSequence[] chars = new CharSequence[list.size()];

		for (int i = 0; i < list.size(); i++)
			chars[i] = list.get(i).getTitle();

		return chars;
	}

}
